package com.hibase.upms.controller;


import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.upms.constant.RoleManagerConstant;
import com.hibase.upms.entity.menu.MdsMenu;
import com.hibase.upms.entity.role.MdsRole;
import com.hibase.upms.entity.role.vo.*;
import com.hibase.upms.service.role.MdsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ChrisCui
 * @since 2019-04-16
 */
@RestController
@RequestMapping("/mds-role")
@Api(tags = "角色管理")
@Slf4j
@RequiredArgsConstructor
public class MdsRoleController {

    private final MdsRoleService roleService;

    @ApiOperation(value = "新建角色", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/addRoleInfo")
    public ResponseModel insertRole(@RequestBody MdsRoleInsertVO roleInsertVO) {
        MdsRoleUpdateVO updateVO = EntityUtil.transform(roleInsertVO, MdsRoleUpdateVO.class);
        roleService.changeRole(updateVO);
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "更新角色", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/updateRoleInfo")
    public ResponseModel updateRole(@RequestBody MdsRoleUpdateVO roleUpdateVO) {
        roleService.changeRole(roleUpdateVO);
        //此处需要返回对象信息
        return ResponseModel.successMsg(roleUpdateVO.getId());
    }

    @ApiOperation(value = "启用角色", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/enableRoleInfo")
    public ResponseModel enableRole(@RequestBody OperationIdsEntity roleIdsVo) {
        roleService.changeRoleStatus(roleIdsVo.getIds(), RoleManagerConstant.RECORD_STATUS_ENABLE);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "停用角色", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/disabledRoleInfo")
    public ResponseModel disabledRole(@RequestBody OperationIdsEntity roleIdsVo) {
        roleService.changeRoleStatus(roleIdsVo.getIds(), RoleManagerConstant.RECORD_STATUS_DISABLED);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "查询角色列表", httpMethod = "POST", response = MdsRoleVO.class)
    @HibasePostRequestMapping(value = "selectList")
    public ResponseModel selectList(@RequestBody MdsRoleSearchVO searchVO) {
        PageInfo result = roleService.selectList(searchVO);
        result.setList(EntityUtil.transform(result.getList(), MdsRoleVO.class));
        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "查询角色详情", httpMethod = "POST", response = MdsRoleViewVO.class)
    @HibasePostRequestMapping(value = "viewDetails")
    public ResponseModel viewDetails(@RequestBody OperationIdEntity idEntity) {
        MdsRole mdsRole = roleService.getById(idEntity.getId());
        MdsRoleViewVO mdsRoleViewVo = EntityUtil.transform(mdsRole, MdsRoleViewVO.class);
        return ResponseModel.successMsg(mdsRoleViewVo);
    }

    @ApiOperation(value = "查询角色绑定用户列表", httpMethod = "POST", response = MdsRoleUserViewVO.class)
    @HibasePostRequestMapping(value = "viewUserDetails")
    public ResponseModel viewUserDetails(@RequestBody OperationIdEntity idEntity) {
        PageInfo result = roleService.selectRoleUserList(idEntity);
        result.setList(EntityUtil.transform(result.getList(), MdsRoleUserViewVO.class));
        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "绑定或移除角色用户", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/addRoleUser")
    public ResponseModel insAndDelRoleUser(@RequestBody MdsRoleUserOpVO mdsRoleUserOpVo) {
        roleService.insAndDelRoleUser(mdsRoleUserOpVo.getRoleId(),mdsRoleUserOpVo.getUserId());
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "查询角色绑定菜单列表", httpMethod = "POST", response = MdsRoleMenuViewVO.class)
    @HibasePostRequestMapping(value = "selectRoleMenuList")
    public ResponseModel selectRoleMenuList(@RequestBody OperationIdEntity idEntity) {
        List<MdsMenu> mdsMenuS = roleService.selectRoleMenuList(idEntity);
        List<MdsRoleMenuViewVO> mdsRoleMenuViewVo = EntityUtil.transform(mdsMenuS, MdsRoleMenuViewVO.class);
        return ResponseModel.successMsg(mdsRoleMenuViewVo);
    }

    @ApiOperation(value = "绑定与移除角色菜单", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/addRoleMenu")
    public ResponseModel insAndDelRoleMenu(@RequestBody MdsRoleMenuOpVO mdsRoleMenuOpVo) {
        roleService.insAndDelRoleMenu(mdsRoleMenuOpVo);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "刷新角色权限", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/refreshPermissions")
    public ResponseModel refreshPermissions() {
        roleService.refreshPermissions();
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "查询当前用户下的角色列表(包含所选用户的已拥有角色)", httpMethod = "POST", response = MdsRoleVO.class)
    @HibasePostRequestMapping(value = "selectCurrentUserRoleList")
    public ResponseModel selectCurrentUserRoleList(@RequestBody OperationIdEntity idEntity) {
        PageInfo result = roleService.selectCurrentUserRoleList(idEntity);
        result.setList(EntityUtil.transform(result.getList(), MdsRoleVO.class));
        return ResponseModel.successMsg(result);
    }
}
